Struct
Option<T>
union struct Option<T>
Namespace: System
Assembly: Raven.Core.dll
Source file: Option.rvn
Inheritance: object → ValueType → Option<T>
Implements: IPropagatable<Option<T>, T, ()>
Represents some value, or no value.
Usage
Use Option like so:
func Test(v: int) -> Option<int> {
if v > 3 {
return Some(42)
}
None
}
Matching result in match expression:
let result = Test(2)
let r = result match {
Some(let value) => "Some: $value"
None => "None"
}
Simple pattern:
if result is Some(let value) {
// Omitted
} else if result is None {
// Omitted
}
Type parameters
| Name | Description |
|---|---|
T |
The type of the optional value. |
Cases
2 cases
case Some(value: T)
Represents an available value.
case None
Represents the absence of a value.
Properties
4 itemsMethods
23 items
func Filter(predicate: T -> bool) -> Option<T>
Alias for `Where`.
static func FromResidual(residual: ()) -> Option<T>
Constructs an empty option from a propagated residual.
func GetEnumerator() -> IEnumerator<T>
Returns an enumerator over the zero-or-one contained values.
func Map<TResult>(mapper: T -> TResult) -> Option<TResult>
Projects the value inside `Some` using `mapper`. `None` stays `None`.
func MapResult<TResult, E>(mapper: T -> TResult, noneError: () -> E) -> Result<TResult, E>
Maps the contained value into a `Result`. If this is `None`, returns `Error(noneError())`.
func Match<TResult>(some: T -> TResult, none: () -> TResult) -> TResult
Matches the option and produces a value.
func OkOr<E>(error: E) -> Result<T, E>
Converts this option into a result, using `error` when it is `None`.
func OkOr<E>(errorFactory: () -> E) -> Result<T, E>
Converts this option into a result, evaluating `errorFactory` when it is `None`.
func OrElse(alternative: () -> Option<T>) -> Option<T>
Returns this option if it is `Some`, otherwise returns `alternative()`.
func Tap(action: T -> ()) -> Option<T>
Runs `action` if this is `Some`, returning the original option.
func TapNone(action: () -> ()) -> Option<T>
Runs `action` if this is `None`, returning the original option.
func Then<TResult>(binder: T -> Option<TResult>) -> Option<TResult>
Monadic bind / flatMap: chains an `Option`-producing function.
func ThenResult<TResult, E>(binder: T -> Result<TResult, E>, noneError: () -> E) -> Result<TResult, E>
Bridges `Option` into a `Result`-producing pipeline. If this is `None`, returns `Error(noneError())`.
func ToEnumerable() -> IEnumerable<T>
Converts `Some(x)` to a single-item sequence and `None` to an empty sequence.
virtual override func ToString() -> string
No summary available.
virtual override func ToString() -> string
Represents some value, or no value.
func TryGetOutput(out output: T) -> bool
Attempts to extract the successful output used by the propagation protocol.
func TryGetResidual(out residual: ()) -> bool
Reports absence as the unit residual used by the propagation protocol.
func UnwrapOr(defaultValue: T) -> T
Returns the contained value, or `defaultValue` when this option is `None`.
func UnwrapOrDefault() -> T
Returns the contained value, or the default value when this option is `None`.
func UnwrapOrElse(factory: () -> T) -> T
Returns the contained value or evaluates `factory()`.
func UnwrapOrThrow() -> T
Returns the contained value, or throws when this option is `None`.
func Where(predicate: T -> bool) -> Option<T>
Keeps the value only if `predicate` returns true.